package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateRepairAssistMapper;
import com.caiheng.api.dao.estate.EstateUserMapper;
import com.caiheng.api.dao.sys.SysDeptMapper;
import com.caiheng.api.dao.sys.SysUserMapper;
import com.caiheng.api.entity.estate.EstateRepairAssist;
import com.caiheng.api.entity.estate.EstateUser;
import com.caiheng.api.entity.sys.SysDept;
import com.caiheng.api.entity.sys.SysUser;
import com.caiheng.api.service.estate.EstateUserService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.sys.SysDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商家的管理用户wxuser的用户Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-27
 */
@Service
public class EstateUserServiceImpl extends BaseServiceImpl<EstateUserMapper, EstateUser> implements EstateUserService {

    @Resource
    private EstateUserMapper estateUserMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private EstateRepairAssistMapper repairAssistMapper;

    @Override
    public EstateUser getEstateUser(int userId) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("state",1);
        qw.last("limit  1");
        return estateUserMapper.selectOne(qw);
    }

    @Override
    public EstateUser getEstateBySysUser(long sysUserId) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("sys_user_id", sysUserId);
        qw.eq("state",1);
        qw.last("limit  1");
        return estateUserMapper.selectOne(qw);
    }

    @Override
    public EstateUser getEstateByUser(int userId, int estateId) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("estate_id",estateId);
        qw.eq("state",1);
        qw.last("limit  1");
        return estateUserMapper.selectOne(qw);
    }

    @Override
    public List<EstateUser> getEstateAndDeptByUser(int record_id, long sysUserId, int estateId) {

        List<Integer> deptIds = new ArrayList<>();
        QueryWrapper<SysDept> qwDept = new QueryWrapper<>();
        qwDept.eq("estate_id", estateId);
        qwDept.eq("event_type", 1);
        List<SysDept> deptList = sysDeptMapper.selectList(qwDept);
        if(CollUtil.isNotEmpty(deptList)){
            for (SysDept dept : deptList){
                deptIds.add(dept.getDeptId());
            }
        }

        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("estate_id",estateId);
        if(deptIds.size() > 0){
            qw.in("dept_id",deptIds);
        }
        if(sysUserId > 0){
            qw.ne("sys_user_id", sysUserId);
        }
        qw.eq("state",1);
        List<EstateUser> estateUsers = estateUserMapper.selectList(qw);
        if(CollUtil.isNotEmpty(estateUsers)){
            for (EstateUser estateUser : estateUsers){
                SysDept dept = sysDeptMapper.selectById(estateUser.getDeptId());
                if(dept != null){
                    estateUser.setDeptName(dept.getDeptName());
                }

                estateUser.setIsCheck(0);
                QueryWrapper<EstateRepairAssist> qwAssist = new QueryWrapper<>();
                qwAssist.eq("record_id", record_id);
                qwAssist.eq("sys_user_id", estateUser.getSysUserId());
                qwAssist.last("limit 1");
                EstateRepairAssist repairAssist = repairAssistMapper.selectOne(qwAssist);
                if(repairAssist!= null){
                    estateUser.setIsCheck(1);
                }
            }
        }
        return estateUsers;
    }

    @Override
    public EstateUser getEstateManagerByUser(int estateId,int isManager) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("estate_id",estateId);
        qw.eq("state",1);
        qw.eq("is_manager",isManager);
        qw.last("limit  1");
        return estateUserMapper.selectOne(qw);
    }

    @Override
    public List<EstateUser> getEstateListByUserId(int userId) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("state",1);
        qw.groupBy("estate_id");
        return estateUserMapper.selectList(qw);
    }

    @Override
    public EstateUser getEstateByManagerUser(int userId) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("state",1);
        qw.eq("is_manager",1);
        qw.last("limit  1");
        return estateUserMapper.selectOne(qw);
    }

    @Override
    public long getManagerCount(int userId, int isManager) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.select("id");
        qw.eq("state",1);
        qw.eq("is_manager",isManager);
        return estateUserMapper.selectCount(qw);
    }

    @Override
    public long getUserCountByUserType(int userId, int userType) {
        QueryWrapper<EstateUser> qw = new QueryWrapper<>();
        qw.select("id");
        qw.eq("user_id",userId);
        qw.eq("state",1);
        qw.eq("user_type",userType);
        return estateUserMapper.selectCount(qw);
    }

    @Override
    public List<EstateUser> selectEstateUserList(Map<String, Object> map) {
        return estateUserMapper.selectEstateUserList(map);
    }
}
